/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package GUI;

import GUI.Components.*;
import LogParser.CommandsHistory;
import Tools.CMDThreadGUI;
import Tools.FactorySingletonCommandGUI;
import Tools.TModel;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 *
 * @author Иван
 */
public class Form extends AObserverGUI {

    private TModel model;
    private JMenuBar menuBar;
    private JPanel browserPanel;
    private JLabel lBrowserPuth,
            rBrowserPuth,
            lBrowserInfo,
            rBrowserInfo;
    private AComponentBuilder panelButtonsCommands,
            panelButtonsDriversLEFT,
            panelButtonsDriversRIGHT,
            panelCMD;
    private ABrowserBuilder browserLEFT,
            browserRIGHT,
            browserTreeLEFT,
            browserTreeRIGHT;

    public Form() {
        getGUI().setLayout(new GridBagLayout());
        getGUI().setTitle("My Commander");
        //setSize(Toolkit.getDefaultToolkit().getScreenSize());
        getGUI().setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        
        getGUI().setMinimumSize(new Dimension(getSize()));
        getGUI().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {

            model = TModel.getInstance();
            model.setInitCommandFactory(new FactorySingletonCommandGUI());

            panelButtonsCommands = new PanelCommandsButtons(model, this);

            panelButtonsDriversLEFT = new PanelButtonsDrivers(model, this);
            panelButtonsDriversRIGHT = new PanelButtonsDrivers(model, this);
            panelCMD = new PanelCMD(model, this, new CommandsHistory("app.log"));

            browserLEFT = new BrowserDir(model, this);
            browserRIGHT = new BrowserDir(model, this);
            browserTreeLEFT = new BrowserTree(model, this);
            browserTreeRIGHT = new BrowserTree(model, this);

        } catch (Exception e) {
             JOptionPane.showConfirmDialog(getGUI().getRootPane(), e.getMessage(), "Error", JOptionPane.ERROR, JOptionPane.ERROR_MESSAGE);
        }



        browserPanel = new JPanel(new GridBagLayout());

    }

    private void initMenuBar() {

        menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenuItem fileExit = new JMenuItem("Exit");
        fileExit.setToolTipText("Exit application");

        fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
                ActionEvent.CTRL_MASK));

        fileExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        file.addSeparator();
        file.add(fileExit);

        menuBar.add(file);

        getGUI().setJMenuBar(menuBar);

        getGUI().setTitle("Submenu");
        getGUI().setSize(360, 250);
        getGUI().setLocationRelativeTo(null);
        getGUI().setDefaultCloseOperation(getGUI().EXIT_ON_CLOSE);
    }

    private void initBrowsers() throws Exception {

        lBrowserPuth = new JLabel();
        rBrowserPuth = new JLabel();

        browserLEFT.setBrowserDirectory(model.getWorkDirectory());
        browserRIGHT.setBrowserDirectory(model.getWorkDirectory());

        lBrowserInfo = new JLabel("info");
        rBrowserInfo = new JLabel("info");

        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.insets.set(0, 10, 10, 10);
        browserPanel.add(panelButtonsDriversLEFT.buildComponent(), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.insets.set(0, 10, 10, 10);
        browserPanel.add(panelButtonsDriversRIGHT.buildComponent(), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.insets.set(0, 10, 0, 10);
        browserPanel.add(lBrowserPuth, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        c.insets.set(0, 10, 0, 10);
        browserPanel.add(rBrowserPuth, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.insets.set(10, 10, 10, 10);
        browserPanel.add(browserTreeLEFT.buildComponent(), c);
        browserTreeLEFT.hideBrowser();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.insets.set(10, 10, 10, 10);
        browserPanel.add(browserLEFT.buildComponent(), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        c.insets.set(10, 10, 10, 10);
        browserPanel.add(browserRIGHT.buildComponent(), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        c.insets.set(10, 10, 10, 10);
        browserPanel.add(browserTreeRIGHT.buildComponent(), c);
        browserTreeRIGHT.hideBrowser();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        c.insets.set(0, 10, 10, 10);
        browserPanel.add(lBrowserInfo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        c.insets.set(0, 10, 10, 10);
        browserPanel.add(rBrowserInfo, c);

    }

    private void initGUI() throws Exception {
        initMenuBar();
        initBrowsers();

        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        getGUI().add(browserPanel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.insets.set(5, 0, 5, 0);
        getGUI().add(panelCMD.buildComponent(), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;

        getGUI().add(panelButtonsCommands.buildComponent(), c);

        browserLEFT.Activate();
        browserRIGHT.Activate();

        setCommandThread(new CMDThreadGUI("cd " + model.getWorkDirectory(), getObserver()));
    }

    @Override
    public void updateView(String processedCommand, Object parameters) {
        panelCMD.updateComponent(null);

        if ("dir".equals(processedCommand)) {
            if (!browserTreeLEFT.isActiv() && !browserLEFT.getVisibleState()) {
                browserLEFT.showBrowser();
                browserLEFT.setBrowserDirectory(model.getWorkDirectory());
            } else if (!browserTreeRIGHT.isActiv() && !browserRIGHT.getVisibleState()) {
                browserRIGHT.showBrowser();
                browserRIGHT.setBrowserDirectory(model.getWorkDirectory());
            }

            if (panelButtonsDriversLEFT.isActiv() || panelButtonsDriversRIGHT.isActiv()) {
                if (panelButtonsDriversLEFT.isActiv()) {
                    browserLEFT.updateComponent(parameters);
                    browserLEFT.setBrowserDirectory(model.getWorkDirectory());
                    panelButtonsDriversLEFT.Disable();
                } else {
                    browserRIGHT.updateComponent(parameters);
                    browserRIGHT.setBrowserDirectory(model.getWorkDirectory());
                    panelButtonsDriversRIGHT.Disable();
                }
            } else {
                if (browserLEFT.isActiv()) {
                    browserLEFT.updateComponent(parameters);
                }

                if (browserRIGHT.isActiv()) {
                    browserRIGHT.updateComponent(parameters);
                }
            }
        } else if ("tree".equals(processedCommand) || "find".equals(processedCommand)) {
            if (browserLEFT.isActiv()) {

                browserLEFT.hideBrowser();
                browserTreeLEFT.showBrowser();
                browserTreeLEFT.Activate();

                browserTreeLEFT.updateComponent(parameters);

            } else {

                browserRIGHT.hideBrowser();
                browserTreeRIGHT.showBrowser();
                browserTreeRIGHT.Activate();

                browserTreeRIGHT.updateComponent(parameters);
            }
        } else {
            try {
                String puth = "dir";
                if (processedCommand.equals("copy") || processedCommand.equals("del") || processedCommand.equals("move")) {
                    if (browserLEFT.isActiv()) {
                        puth += " " + browserLEFT.getBrowserDirectory();
                    } else if (browserRIGHT.isActiv()) {
                        puth += " " + browserRIGHT.getBrowserDirectory();
                    }
                }

                setCommandThread(new CMDThreadGUI(puth, getObserver()));
                getCommandThread().t.join();

            } catch (InterruptedException ex) {
                JOptionPane.showConfirmDialog(getGUI().getRootPane(), ex.getMessage(), "Error", JOptionPane.ERROR, JOptionPane.ERROR_MESSAGE);
            }
        }

        lBrowserPuth.setText(browserLEFT.getBrowserDirectory());
        rBrowserPuth.setText(browserRIGHT.getBrowserDirectory());
    }

    private AObserverGUI getObserver() {
        return this;
    }

    @Override
    public String buildCommand(String commandName) {
        String resultCommand = commandName;
        if (commandName.equals("copy")) {

            if (this.getObserver().getSourceDirectory().equals(browserLEFT.getSelectedItem())) {
                getObserver().setSourceDirectory(browserLEFT.getBrowserDirectory() + "\\" + browserLEFT.getSelectedItem());
                getObserver().setDestinationDirectory(browserRIGHT.getBrowserDirectory());

                browserRIGHT.Activate();
            } else {
                getObserver().setSourceDirectory(browserRIGHT.getBrowserDirectory() + "\\" + browserRIGHT.getSelectedItem());
                getObserver().setDestinationDirectory(browserLEFT.getBrowserDirectory());

                browserLEFT.Activate();
            }
            resultCommand = commandName + " " + getObserver().getSourceDirectory() + " " + getObserver().getDestinationDirectory();
        } else if (commandName.equals("move")) {

            if (this.getObserver().getSourceDirectory().equals(browserLEFT.getSelectedItem())) {
                getObserver().setSourceDirectory(browserLEFT.getBrowserDirectory() + "\\" + browserLEFT.getSelectedItem());
                getObserver().setDestinationDirectory(browserRIGHT.getBrowserDirectory());

                browserLEFT.Activate();
            } else {
                getObserver().setSourceDirectory(browserRIGHT.getBrowserDirectory() + "\\" + browserRIGHT.getSelectedItem());
                getObserver().setDestinationDirectory(browserLEFT.getBrowserDirectory());

                browserRIGHT.Activate();
            }

            resultCommand = commandName + " " + getObserver().getSourceDirectory() + " " + getObserver().getDestinationDirectory();

        } else if (commandName.equals("del")) {
            if (this.getObserver().getSourceDirectory().equals(browserLEFT.getSelectedItem())) {

                resultCommand = commandName + " " + browserLEFT.getBrowserDirectory() + "\\" + browserLEFT.getSelectedItem();

                browserLEFT.Activate();
            } else {

                resultCommand = commandName + " " + browserRIGHT.getBrowserDirectory() + "\\" + browserRIGHT.getSelectedItem();

                browserRIGHT.Activate();
            }
        } else if (commandName.equals("tree")) {
            if (browserLEFT.getBrowserDirectory().equals(model.getWorkDirectory())) {

                browserLEFT.Activate();
            } else {

                browserRIGHT.Activate();
            }

        } else if (commandName.equals("find")) {
            if (browserLEFT.getBrowserDirectory().equals(model.getWorkDirectory())) {

                browserLEFT.Activate();
                resultCommand += " " + JOptionPane.showInputDialog(this.getGUI(), "enter mask of search","find",JOptionPane.INFORMATION_MESSAGE);

            } else {
                browserRIGHT.Activate();
            }
        } else if (commandName.equals("cd")) {
            
            resultCommand += " " + JOptionPane.showInputDialog(this.getGUI(), "enter directory","cd",JOptionPane.INFORMATION_MESSAGE);
            
        }else if (commandName.equals("mkdir")) {
            
            resultCommand += " " + JOptionPane.showInputDialog(this.getGUI(), "enter name of folder","mkdir",JOptionPane.INFORMATION_MESSAGE);
            
        }else if (commandName.equals("dir")) {
            
            resultCommand += " " + JOptionPane.showInputDialog(this.getGUI(), "enter puth","dir",JOptionPane.INFORMATION_MESSAGE);
        }

        return resultCommand;
    }

    public static void main(String[] arg) {


        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    Form gui = new Form();
                    gui.initGUI();
                    gui.showGUI();
                } catch (Exception e) {
                }

            }
        });
    }
}
